package cn.house.rent.service.impl;

import cn.house.rent.entity.HouseImg;
import cn.house.rent.entity.User;
import cn.house.rent.mapper.HouseMapper;
import cn.house.rent.mapper.OrderMapper;
import cn.house.rent.service.HouseService;
import cn.house.rent.utils.Result;
import cn.house.rent.vo.HouseVo;
import cn.house.rent.vo.TenantHomeVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.List;

@Service
public class HouseServiceImpl implements HouseService {
    @Autowired
    private HouseMapper houseMapper;
    @Autowired
    private OrderMapper orderMapper;

    //我的家模块查询和搜索查询
    @Override
    public Result tenantHome(User user, HouseVo houseVo) {
        PageHelper.startPage(houseVo.getPage(),houseVo.getLimit());
        List<TenantHomeVo> tenantHomeVos = houseMapper.selectTenantHome(user,houseVo);
        for (TenantHomeVo t: tenantHomeVos) {
            if (t.getHouseName() == null) {
                t.setHouseName("已下线");
                t.setHouseAddress("已下线");
                t.setHouseStatus(7);
                t.setHousePrice(0);
                t.setHouseType(10);
            }
            if (t.getOrderStatus() == 2) {
                t.setHouseStatus(8);
            }
        }
        Result result = new Result();
        PageInfo<TenantHomeVo> page = new PageInfo<>(tenantHomeVos);
        if (tenantHomeVos!=null){
            result.setCode(0);
            result.setMsg("查询成功！");
            result.setCount(page.getTotal());
            result.setData(tenantHomeVos);
        }else {
            result.setCode(500);
            result.setMsg("查询失败！");
        }
        return result;
    }

    //我的家退租  修改房屋状态
    @Override
    public Result surrTenant(TenantHomeVo tenantHomeVo) {
        tenantHomeVo.setUpdateHouseStatus(6);
        int line = houseMapper.updateHouseStatusById(tenantHomeVo);
        if (line>0){
            return Result.builder().code(0).msg("退租已申请！").build();
        }else {
            return Result.builder().code(0).msg("退租申请失败！").build();
        }
    }

    //我的家删除  将租客订单表中对应的删除
    @Override
    public Result delTenantHome(TenantHomeVo tenantHomeVo) {
        int line = orderMapper.deleteTenantOrderById(tenantHomeVo.getId());
        if (line>0){
            return Result.builder().code(0).msg("删除成功！").build();
        }else {
            return Result.builder().code(500).msg("删除失败！").build();
        }
    }

    //根据用户id 查询该用户所有发布的房源信息
    @Override
    public Result selectByLandIdHome(User user, HouseVo houseVo) {
        PageHelper.startPage(houseVo.getPage(), houseVo.getLimit());
        List<HouseVo> houseVos = houseMapper.selectByLandId(user, houseVo);
        PageInfo<HouseVo> page = new PageInfo<>(houseVos);
        if (houseVos != null) {
            return Result.builder().code(0).msg("查询成功").data(houseVos).count(page.getTotal()).build();
        } else {
            return Result.builder().code(500).msg("查询失败").build();
        }
    }

    //根据房屋id 查询房屋所有信息
    @Override
    public Result getHouseAllById(Integer houseId) {
        HouseVo houseVo = houseMapper.getHouseAllById(houseId);
        if (houseVo != null) {
            return Result.builder().code(0).msg("查询成功").data(houseVo).build();
        } else {
            return Result.builder().code(500).msg("查询失败").build();
        }
    }

    //根据条件查房屋信息
    @Override
    public Result getHouseByInfo(HouseVo houseVo) {
        List<HouseVo> houseByInfo = houseMapper.getHouseByInfo(houseVo);
        if (houseVo != null) {
            return Result.builder().code(0).msg("查询成功").data(houseByInfo).build();
        } else {
            return Result.builder().code(500).msg("查询失败").build();
        }
    }

    //房东  房子管理  退租功能
    @Override
    public Result surrLand(TenantHomeVo tenantHomeVo) {
        tenantHomeVo.setUpdateHouseStatus(5);
        int line = houseMapper.updateHouseStatusById(tenantHomeVo);
        if (line>0){
            return Result.builder().code(0).msg("已退租！").build();
        }else {
            return Result.builder().code(0).msg("退租失败！").build();
        }
    }

    //房东  房子管理  删除功能
    @Override
    public Result delLandHome(TenantHomeVo tenantHomeVo) {
        int line = houseMapper.delHouseById(tenantHomeVo);
        List<HouseImg> houseImgById = houseMapper.getHouseImgById(tenantHomeVo.getHouseId());
        for (HouseImg h:houseImgById){
            new File("F:\\project\\ideaprojects\\houseRentSys\\src\\main\\webapp\\static\\images\\houseimgs",h.getHouseImg()).delete();
        }
        int imgLine = houseMapper.delHouseByIdImg(tenantHomeVo);
        if (imgLine > 0 && line > 0) {
            return Result.builder().code(0).msg("删除成功！").build();
        }else {
            return Result.builder().code(500).msg("删除失败！").build();
        }
    }

    //房东  房子管理  同意退租功能
    @Override
    public Result passHome(TenantHomeVo tenantHomeVo) {
        tenantHomeVo.setUpdateHouseStatus(5);
        int line = houseMapper.updateHouseStatusById(tenantHomeVo);
        if (line > 0){
            return Result.builder().code(0).msg("处理成功！").build();
        }else {
            return Result.builder().code(500).msg("处理失败！").build();
        }
    }

    //房东  房子管理  驳回退租功能
    @Override
    public Result rejectHome(TenantHomeVo tenantHomeVo) {
        tenantHomeVo.setUpdateHouseStatus(4);
        int line = houseMapper.updateHouseStatusById(tenantHomeVo);
        if (line > 0){
            return Result.builder().code(0).msg("处理成功！").build();
        }else {
            return Result.builder().code(500).msg("处理失败！").build();
        }
    }

    //发布房源
    @Override
    public Result addHouse(User user, HouseVo houseVo) {
        String houseImg = houseVo.getHouseImg().substring(1);
        houseVo.setHouseStatus(7);
        houseVo.setParentId(user.getUserId());
        int line = houseMapper.addHouse(houseVo);
        String[] houseImgs = houseImg.split(",");
        int imgLine = houseMapper.addHouseImg(houseImgs, houseVo.getHouseId());
        if (line > 0 && imgLine > 0) {
            return Result.builder().code(0).msg("发布成功！").build();
        }else {
            return Result.builder().code(500).msg("发布失败！").build();
        }
    }

    //房东  房子管理  编辑功能
    @Override
    public Result updateHouse(HouseVo houseVo) {
        List<HouseImg> houseImgById = houseMapper.getHouseImgById(houseVo.getHouseId());
        for (HouseImg h:houseImgById) {
            new File("F:\\project\\ideaprojects\\houseRentSys\\src\\main\\webapp\\static\\images\\houseimgs",h.getHouseImg()).delete();
        }
        int houseLine = houseMapper.updateHouseById(houseVo);
        String houseImg = houseVo.getHouseImg().substring(1);
        String[] houseImgs = houseImg.split(",");
        int imgLine = houseMapper.addHouseImg(houseImgs, houseVo.getHouseId());
        if (houseLine > 0 && imgLine > 0) {
            return Result.builder().code(0).msg("修改成功！").build();
        }else {
            return Result.builder().code(500).msg("修改失败！").build();
        }
    }
}
